<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width = device-width ,initial-scale = 1,minimum-scale = 1,maximum-scale = 1,user-scalable =no,"/>
    <title>绑定数据 - {$maccms.site_name}</title>
    {include file="user/include" /}
</head>
<body>
<!--子页顶部-->
<div class="zy-search zy-tit">
    <div class="sch-w clearfix">
        <a class="go-back" href="#"><img src="{$maccms.path_tpl}images/user/go-back.png"></a>
        <div class="tit">
            用户资料---绑定{if condition="$ac eq 'phone'"}手机{else/}邮箱{/if}
        </div>
    </div>
</div>
<div class="information-w">
    <!--个人信息-->
    <form id="fm" name="fm" method="post" action="" >
        <ul class="clearfix">
            <input type="hidden" name="ac" value="{$ac}">
            <li class="no-bg"><span class="tit">{if condition="$ac eq 'phone'"}手机{else/}邮箱{/if}：</span><span><input type="text" name="to" class="new-val" value=""></span><input type="button" id="btn_bind_send" value="获取验证码"/></li>
            <li class="no-bg"><span class="tit">验证码：</span><span><input type="text" name="code" class="new-val" value=""></span></li>
            <li><a class="btn-brand" href="javascript:;" id="btn_submit" style="width: 60%">确认绑定</a></li>
        </ul>
    </form>
</div>

<!--loading start-->
<div class="loading_box"></div>
<!--loading end-->

<script>



    var countdown=60;
    function settime(val) {
        if (countdown == 0) {
            val.removeAttribute("disabled");
            val.value="获取验证码";
            countdown = 60;
            return true;
        } else {
            val.setAttribute("disabled", true);
            val.value="重新发送(" + countdown + ")";
            countdown--;
        }
        setTimeout(function() {settime(val) },1000)
    }

    $('#btn_bind_send').click(function(){
        var ac = $('input[name="ac"]').val();
        var to = $('input[name="to"]').val();
        if(ac=='email') {
            var pattern = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
            var ex = pattern.test(to);
            if (!ex) {
                alert('邮箱格式不正确');
                return;
            }
        }
        else if(ac=='phone') {
            var pattern=/^[1][0-9]{10}$/;
            var ex = pattern.test(to);
            if (!ex) {
                alert('手机号格式不正确');
                return;
            }
        }
        else{
            alert('参数错误');
            return;
        }


        settime(this);
        var data = $("#fm").serialize();

        $.ajax({
            url: "{:url('user/bindmsg')}",
            type: "post",
            dataType: "json",
            data: data,
            beforeSend: function () {
                //开启loading
            },
            success: function (r) {
                alert(r.msg);
            },
            complete: function () {
                //结束loading
            }
        });
    });

    $("#btn_submit").click(function() {
        var ac = $('input[name="ac"]').val();
        var to = $('input[name="to"]').val();

        if(ac=='email') {
            var pattern = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
            var ex = pattern.test(to);
            if (!ex) {
                alert('邮箱格式不正确');
                return;
            }
        }
        else if(ac=='phone') {
            var pattern=/^[1][0-9]{10}$/;
            var ex = pattern.test(to);
            if (!ex) {
                alert('手机号格式不正确');
                return;
            }
        }
        else{
            alert('参数错误');
            return;
        }

        var code = $('input[name="code"]').val();
        if(code==''){
            alert('请输入验证码');
            return;
        }
        var data = $("#fm").serialize();

        $.ajax({
            url: "{:url('user/bind')}",
            type: "post",
            dataType: "json",
            data: data,
            beforeSend: function () {
                //开启loading
                //$(".loading_box").css("display","block");
                $("#btn_submit").css("background","#fd6a6a").val("loading...");
            },
            success: function (r) {
                alert(r.msg);
                if(r.code==1){
                    location.href="{:url('user/info')}";
                }
            },
            complete: function () {
                //结束loading
                //$(".loading_box").css("display","none");
                $("#btn_submit").css("background","#fa4646").val("提交");
            }
        });
    });


    $(".go-back").click(function () {
        var ref = document.referrer;
        location.href=ref;
    })

</script>
</body>
</html>